home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr05
/
xnot12a.zip
/
TTYIO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-20
|
5KB
|
281 lines
#include "jam.h"
/*
* Name: Mg 2b
* MSDOS terminal I/O (TurboC 1.5)
*
* Massively modified Nov 1992 for Windows version
* Julie A Melbin
*
* The functions in this file
* negotiate with the operating system for
* keyboard characters, and write characters to
* the display
*
* This version goes along with tty/ibmbios/tty.c.
* Terminal size is determined there, rather than here.
*/
#include "def.h"
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#ifndef WINDOWED
# ifndef CURSES
void * calloc(size_t, size_t);
void exit(int);
# endif
#endif
static int ttyactivep = FALSE; /* terminal in editor mode? */
int nrow = 0; /* Terminal size, rows. */
int ncol = 0; /* Terminal size, columns. */
/*
* This function gets called once, to set up
* the terminal channel. This is essentially a no-op
* on msdos, since I/O will all be done via bios calls.
*/
ttopen()
{
if (!ttyactivep)
{
#ifndef WINDOWED
signal(SIGINT, SIG_IGN);
InitInput();
#endif
#ifndef WINDOWED /* WinMain sets this up */
nrow = 25; /* initial guess */
ncol = 80;
#endif
ttyactivep = TRUE;
}
return(1);
}
ttwindowsize(f,n)
int f,n;
{
#ifdef WINDOWED
char windowsize[NLINE];
int s, row, col;
s = eread("New rows,cols:", windowsize, NLINE, EFNEW);
if (s != TRUE)
return (s);
sscanf(windowsize, "%d,%d", &row,&col);
WindowNewSize(row,col);
return(TRUE);
#else
ewprintf("NYI");
return(FALSE);
#endif
}
/*
* This function gets called just
* before we go back home to the shell. Another
* MSDOS no_op.
*/
ttclose()
{
if (ttyactivep)
ttyactivep = FALSE;
return(1);
}
/********************************************************************/
/* ttputc, ttgetc & typeahead have been deleted from this file, and */
/* moved into the tty specific code. There is no operating-system */
/* generic way to do these */
/********************************************************************/
/*
* Flush output.
*/
ttflush(force)
BOOL force;
{
#ifdef WINDOWED
WindowFlush();
if (force)
WindowSync();
#endif
#ifdef CURSES
wrefresh(stdscr);
#endif
return TRUE;
}
BOOL ttfatal()
{
#ifdef WINDOWED
return (WindowFatalState());
#else
return (FALSE);
#endif
}
#ifndef WINDOWED
# define MAXCHARS 512
static KCHAR *kbdinput = 0;
static KCHAR *putback_kbdinput = 0;
static int kbdcount = 0;
static int putback_kbdcount = 0;
/* alloc the internal event queues
*/
void InitInput()
{
if (!(kbdinput = (KCHAR *)calloc(MAXCHARS, sizeof(KCHAR))))
printf("Unable to init kbdinput");
if (!(putback_kbdinput = (KCHAR *)calloc(MAXCHARS, sizeof(KCHAR))))
printf("Unable to init putback_kbdinput");
}
/* putback kchar to to input queue
*/
void PutbackKchar(c)
KCHAR c;
{
if (putback_kbdcount < MAXCHARS)
{
putback_kbdinput[putback_kbdcount] = c;
putback_kbdcount++;
}
}
/* if putback'ed events, push to normal input stream now
*/
void CheckForPutback()
{
int i, j;
/* if not enough room, die
*/
if ((MAXCHARS - kbdcount) < putback_kbdcount)
putback_kbdcount = 0;
else
for (j = putback_kbdcount, i = 0; i < j; i++)
{
AddKchar(putback_kbdinput[i]);
putback_kbdcount--;
}
}
/* push normal string to input buf
*/
void AddString(s)
char *s;
{
while (s && *s)
AddKchar((KCHAR)*s++);
}
/* add kchar to internal queue of collected/manufactured events
*/
void AddKchar(c)
KCHAR c;
{
if (kbdcount < MAXCHARS)
{
kbdinput[kbdcount] = c;
kbdcount++;
}
else
ttbeep();
}
/* return first inqueued char from event into
* pkchar (if NON_NULL) and return status
*/
BOOL DOSReturnKCHAR(pkchar)
KCHAR *pkchar;
{
register int i;
BOOL result = FALSE;
/* if something, remove from queue head
* and push the queue down
* (should use head/tail pointers!)
*/
if (kbdcount > 0)
{
result = TRUE;
if (pkchar)
{
*pkchar = kbdinput[0];
kbdcount--;
for (i = 0; i < kbdcount; i++)
kbdinput[i] = kbdinput[i+1];
}
}
return(result);
}
#endif /* WINDOWED */
/* suck up (and throw away if flag set) any pending input
*/
void ttflushinput(flag)
int flag;
{
KCHAR kchar;
#ifdef WINDOWED
while (WindowReturnKCHAR(&kchar))
if (!flag)
PutbackKchar(kchar);
#else
while (DOSReturnKCHAR(&kchar))
if (!flag)
PutbackKchar(kchar);
#endif
}
/*
* PANIC: print error and die, leaving incremental saves
* and the log file.
*/
void panic(s)
char *s;
{
#ifdef MSW
char buff[512];
sprintf(buff, "Panic: %s", s);
WindowMessage(buff, TRUE);
#else
fprintf(stderr, "%s\r\n", s);
exit(1);
#endif
}
void tteol()
{
tteeol();
}
/*
** This should check the size of the window, and reset if needed.
*/
setttysize()
{
#ifdef WINDOWED
WindowGetSize(&nrow, &ncol);
#else
nrow = 25;
ncol = 80;
#endif
return TRUE;
}